﻿using System.Threading.Tasks;
using System.Web.Mvc;
using ScrumPilot.Domain.BPL;
using ScrumPilot.Helpers;

namespace ScrumPilot.Controllers
{
    [Authorize]
    public partial class HomeController
    {
        private KanbanDashBoardServices _kanbanDashBoardServices;
        protected KanbanDashBoardServices KanbanDashBoardServices { get { return _kanbanDashBoardServices ?? (_kanbanDashBoardServices = new KanbanDashBoardServices(TFS,this.TfsRootUrl)); } }

        [HttpGet, NoCache]
        public async Task<JsonResult> JBugsList()
        {
            return Json(
                await ComputeKPIBugs(), JsonRequestBehavior.AllowGet
            );
        }

        // ReSharper disable CSharpWarnings::CS1998
        private async Task<KanbanDashBoardServices.KPIBugs> ComputeKPIBugs()
        // ReSharper restore CSharpWarnings::CS1998
        {
            return TFS != null ? KanbanDashBoardServices.GetKPIBusgs() : null;
        }

        [HttpGet, NoCache]
        public async Task<JsonResult> JKanbanProjectsList()
        {
            return Json(
                await GetKanbanProjects(), JsonRequestBehavior.AllowGet
            );
        }

        // ReSharper disable CSharpWarnings::CS1998
        private async Task<KanbanDashBoardServices.KanbanMainTable> GetKanbanProjects()
        // ReSharper restore CSharpWarnings::CS1998
        {
            return TFS != null ? KanbanDashBoardServices.KanbanProjects() : null;
        }

        [HttpGet, NoCache]
        public async Task<JsonResult> JKPIProjects()
        {
            return Json(
             await GetKPIProjects(), JsonRequestBehavior.AllowGet
           );
        }

        // ReSharper disable CSharpWarnings::CS1998
        private async Task<KanbanDashBoardServices.KPIKanbanProjects> GetKPIProjects()
        // ReSharper restore CSharpWarnings::CS1998
        {
            return TFS != null ? KanbanDashBoardServices.KPIProjects() : null;
        }

        [HttpGet, NoCache]
        public JsonResult JKPIItems(string iteration, byte state)
        {
            return Json(KanbanDashBoardServices.GetWorkItems(iteration, state), JsonRequestBehavior.AllowGet);
        }
    }
}
